/*
 * Copyright 2016 Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mingmen.supplier.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.mingmen.supplier.R;
import com.mingmen.supplier.adapter.ResturantAdapter;
import com.mingmen.supplier.defineview.ClearEditText;
import com.mingmen.supplier.model.RestaurantBean;
import com.mingmen.supplier.util.PinnedHeaderDecoration;
import com.mingmen.supplier.util.Trans2PinYinUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;


/**
 * <p>
 * ListView形式的侧滑菜单，支持横向和竖向的。
 * </p>
 * Created by Yan Zhenjie on 2016/7/27.
 */
public class DistributionModeActivity extends Activity {

    @BindView(R.id.btn_ziqu)
    Button btn_ziqu;
    @BindView(R.id.btn_peisong)
    Button btn_peisong;
    String shipping;
    @BindView(R.id.btn_receive)
    Button btn_receive;
    String shippingFee;
    @BindView(R.id.shipping_send)
    EditText shipping_send;
    String text_peiSong = "";
    String flag = "";
    @BindView(R.id.layout_shippFree)
    LinearLayout layout_shippFree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_distribution);

        ButterKnife.bind(this);
        shipping = getIntent().getStringExtra("shipping");
        shippingFee = getIntent().getStringExtra("shippingFee");
        flag = getIntent().getStringExtra("flag");
        shipping_send.setText(shippingFee);
        if (shipping.equals("0")) {
            btn_ziqu.setBackground(getResources().getDrawable(R.drawable.shape_blue_bluewhitenormal));
            btn_peisong.setBackground(getResources().getDrawable(R.drawable.shape_jinglii_dingdan_item));
            text_peiSong = "自取";
            btn_ziqu.setTextColor(Color.parseColor("#62BF73"));
            btn_peisong.setTextColor(Color.parseColor("#A0A0A0"));
            layout_shippFree.setVisibility(View.GONE);
        } else if (shipping.equals("1")) {
            btn_ziqu.setBackground(getResources().getDrawable(R.drawable.shape_jinglii_dingdan_item));
            btn_peisong.setBackground(getResources().getDrawable(R.drawable.shape_blue_bluewhitenormal));
            text_peiSong = "商家配送";
            btn_ziqu.setTextColor(Color.parseColor("#A0A0A0"));
            btn_peisong.setTextColor(Color.parseColor("#62BF73"));
            layout_shippFree.setVisibility(View.VISIBLE);
        } else {
         /*   btn_ziqu.setBackground(getResources().getDrawable(R.drawable.shape_blue_bluewhitenormal));
            btn_peisong.setBackground(getResources().getDrawable(R.drawable.shape_jinglii_dingdan_item));
            btn_ziqu.setTextColor(Color.parseColor("#A0A0A0"));
            btn_peisong.setTextColor(Color.parseColor("#62BF73"));*/
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @OnClick({R.id.btn_ziqu, R.id.btn_peisong, R.id.btn_receive})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.btn_ziqu:

                shipping = "0";
                btn_ziqu.setBackground(getResources().getDrawable(R.drawable.shape_blue_bluewhitenormal));
                btn_peisong.setBackground(getResources().getDrawable(R.drawable.shape_jinglii_dingdan_item));
                btn_ziqu.setTextColor(Color.parseColor("#62BF73"));
                btn_peisong.setTextColor(Color.parseColor("#A0A0A0"));
                text_peiSong = "自取";
                layout_shippFree.setVisibility(View.GONE);
                break;
            case R.id.btn_peisong:
                shipping = "1";
                btn_ziqu.setBackground(getResources().getDrawable(R.drawable.shape_jinglii_dingdan_item));
                btn_peisong.setBackground(getResources().getDrawable(R.drawable.shape_blue_bluewhitenormal));
                text_peiSong = "商家配送";
                btn_ziqu.setTextColor(Color.parseColor("#A0A0A0"));
                btn_peisong.setTextColor(Color.parseColor("#62BF73"));
                layout_shippFree.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_receive:
                if (TextUtils.isEmpty(shipping_send.getText().toString())) {
                    Toast.makeText(this, "配送费不能为空", Toast.LENGTH_SHORT).show();
                } else if (shipping_send.getText().toString().startsWith("0")) {
                    Toast.makeText(this, "输入不规范", Toast.LENGTH_SHORT).show();
                } else {
                    if (flag.equals("0")) {
                        intent = new Intent(DistributionModeActivity.this, NewOrderDetailActivity.class);
                        intent.putExtra("shipping", shipping);
                        intent.putExtra("shippingFee", shipping_send.getText().toString());
                        intent.putExtra("text_peiSong", text_peiSong);
                        setResult(100, intent);
                    } else if (flag.equals("1")) {
                        intent = new Intent(DistributionModeActivity.this, NewDaiPeiSongDetailActivity.class);
                        intent.putExtra("shipping", shipping);
                        intent.putExtra("shippingFee", shipping_send.getText().toString());
                        intent.putExtra("text_peiSong", text_peiSong);
                        setResult(100, intent);
                    }

                }

                finish();
                break;
        }
    }

}